using System;
using System.IO;

namespace muduoDotNet
{
    /// <summary>
    /// 日志级别枚举
    /// </summary>
    public enum LogLevel
    {
        Debug,
        Info,
        Warning,
        Error,
        Fatal
    }

    /// <summary>
    /// 简单的日志记录器
    /// </summary>
    public static class Logger
    {
        private static LogLevel _logLevel = LogLevel.Info;
        private static readonly object _lock = new object();
        private static TextWriter _output = Console.Out;

        /// <summary>
        /// 设置日志级别
        /// </summary>
        /// <param name="level">日志级别</param>
        public static void SetLogLevel(LogLevel level)
        {
            _logLevel = level;
        }

        /// <summary>
        /// 设置日志输出
        /// </summary>
        /// <param name="output">输出流</param>
        public static void SetOutput(TextWriter output)
        {
            _output = output ?? Console.Out;
        }

        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="message">消息</param>
        public static void Debug(string message)
        {
            Log(LogLevel.Debug, message);
        }

        /// <summary>
        /// 记录信息
        /// </summary>
        /// <param name="message">消息</param>
        public static void Info(string message)
        {
            Log(LogLevel.Info, message);
        }

        /// <summary>
        /// 记录警告
        /// </summary>
        /// <param name="message">消息</param>
        public static void Warning(string message)
        {
            Log(LogLevel.Warning, message);
        }

        /// <summary>
        /// 记录错误
        /// </summary>
        /// <param name="message">消息</param>
        public static void Error(string message)
        {
            Log(LogLevel.Error, message);
        }

        /// <summary>
        /// 记录错误
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="exception">异常</param>
        public static void Error(string message, Exception exception)
        {
            Log(LogLevel.Error, $"{message}: {exception.Message}");
        }

        /// <summary>
        /// 记录致命错误
        /// </summary>
        /// <param name="message">消息</param>
        public static void Fatal(string message)
        {
            Log(LogLevel.Fatal, message);
        }

        /// <summary>
        /// 记录致命错误
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="exception">异常</param>
        public static void Fatal(string message, Exception exception)
        {
            Log(LogLevel.Fatal, $"{message}: {exception.Message}");
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="level">日志级别</param>
        /// <param name="message">消息</param>
        private static void Log(LogLevel level, string message)
        {
            if (level < _logLevel)
            {
                return;
            }

            lock (_lock)
            {
                var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                var levelStr = level.ToString().ToUpper().PadRight(7);
                var threadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
                
                _output.WriteLine($"{timestamp} [{levelStr}] [Thread-{threadId}] {message}");
            }
        }
    }
}